package org.ainlolcat.ainscience.UI;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 11/4/11
 * Time: 8:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class XTabbedPane extends JTabbedPane {

    public XTabbedPane(){
        super();
    }

    private class Caption extends JPanel{
        JLabel title;
        JButton close;
        JTabbedPane pane;

        public Caption(JTabbedPane newpane){

            super(new FlowLayout(FlowLayout.LEADING));
            pane = newpane;
            title = new JLabel(){
                public String getText(){
                    int i = pane.indexOfTabComponent(Caption.this);
                    if (i != -1) {
                        return pane.getTitleAt(i);
                    }
                    return null;
                }
            };

            add(title);
            close = new JButton(){

                public void updateUI() {
                }

                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    Graphics2D g2 = (Graphics2D) g.create();
                    int cross_width = 8;
                    g2.drawLine(cross_width, cross_width, getWidth() - cross_width - 1, getHeight() - cross_width - 1);
                    g2.drawLine(getWidth() - cross_width - 1, cross_width, cross_width, getHeight() - cross_width - 1);
                    g2.dispose();
                }
            };
            close.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int i = pane.indexOfTabComponent(Caption.this);
                    if (i != -1) {
                        pane.remove(i);
                    }
                }
            });
            close.setPreferredSize(new Dimension(10,10));
            add(close);
        }

    }

    public void addTab(String name,Component comp){
        //JScrollPane scrollPane = new JScrollPane(comp);
        super.addTab(name,comp);
        setTabComponentAt(indexOfComponent(comp),new Caption(this));
    }
}
